<?php

namespace app\batch;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\models\{User as Users, Address, Riderlist, Order, RiderStreet, Admin};
use think\Facade\Config as Configs;
use app\facade\Mns;
use think\Db;
use app\facade\Redis;
use EasyWeChat\Factory;

class Message extends Command
{
    protected $open = false;
    protected function configure()
    {
        $this->setName('Message')->setDescription('通知');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->doCron();
        die();
    }

    protected function doCron()
    {
        $cmd = "ps aux | grep '/www/server/php/72/bin/php /www/wwwroot/garbage/think Message'|grep -v 'grep'| awk '{print $2}'";
        exec($cmd, $ps, $ret);
        if (count($ps) > 1) {
            die("batch of  is running");
        }
        ini_set('max_execution_time', 0);
        (new Order())->where(['status' => 1, 'message' => 1])->chunk(50, function ($list) {
            foreach ($list as $lists) {
                if ($lists['ridermessage'] == 0) {
                    //管理人员通知
                    $admins = (new Admin())->where('find_in_set(' . $lists['street'] . ',street)')->where(['type' => [3, 4]])->find();
                    if (empty($admins)) {
                        $admins = (new Admin())->where('find_in_set(' . $lists['area'] . ',area)')->where(['type' => 2])->find();
                    }
                    if (!empty($admins)) {
                        Redis::lPush('messagelist_' . $admins['id'], $lists['id']);
                    }
                    //判断当前区域是否有骑手
                    $RiderStreet = (new RiderStreet())->where(['street' => $lists['street'], 'status' => 2])->column('riderid');
                    if (count($RiderStreet) > 0) {
                        $RiderList = (new RiderList())->where(['id' => $RiderStreet, 'status' => 1])->select();
                        foreach ($RiderList as $rists) {
                            if (empty($rists['openid'])) {
                                (new Mns())->sendmessage($rists['mobile'], $lists['ordersn'], $lists['name'], $lists['mobile'], $lists['address'], $lists['appointment']);
                            } else {
                                $rs = wechattest($rists['openid'], '您所属区域有一个新的订单，请速去接单。', $lists['ordersn'], $lists['appointment'], $lists['name'], $lists['address'], '联系电话:' . $lists['mobile']);
                                dump($lists);
                                dump($rs);
                            }
                        }
                    }
                } else {
                    $RiderList = (new RiderList())->where(['id' => $lists['ridermessage']])->select();
                    foreach ($RiderList as $rists) {
                        if (empty($rists['openid'])) {
                            (new Mns())->sendmessage($rists['mobile'], $lists['ordersn'], $lists['name'], $lists['mobile'], $lists['address'], $lists['appointment']);
                        } else {
                            $rs = wechattest($rists['openid'], '您所属区域有一个新的订单，请速去接单。', $lists['ordersn'], $lists['appointment'], $lists['name'], $lists['address'], '联系电话:' . $lists['mobile']);
                        }
                    }
                }
                (new Order())->where(['id' => $lists['id']])->update(['message' => 2]);
            }
        });
    }
}
